package com.bicon.botu.mall.order.api;

import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.order.common.OrderConstans;
import com.bicon.botu.mall.order.dto.OrderGoodsDto;
import com.bicon.botu.mall.order.service.IOrderGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商城订单 前端控制器
 * @author dsp
 * @since 2018-05-14
 */
@RestController
@RequestMapping("/V3.0.0/api/goods-order")
public class OrderGoodsApiV3 extends BaseController {

    @Autowired
    private IOrderGoodsService iOrderGoodsService;

    /**
     * 根据订单状态 查询
     * @param userId
     * @param payState
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "getPageByPayCondition",method = RequestMethod.POST)
    public BaseResult getPageByPayCondition(String userId,Integer payState, String condition,
                                        @RequestParam(defaultValue = "0") Integer pageNo,
                                        @RequestParam(defaultValue = "10") Integer pageSize){
        if(StringUtils.isNotBlank(userId)){
            Page<OrderGoodsDto> page=iOrderGoodsService.getPageByPayCondition(new Page<OrderGoodsDto>(pageNo,pageSize),userId,payState,condition);
            return sendResult200(page);
        }else{
            return sendResult400();
        }
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/deleteOrder",method = RequestMethod.POST)
    public BaseResult deleteOrder(String orderId){
        if(StringUtils.isNotBlank(orderId)){
            if(iOrderGoodsService.deleteOrder(orderId,getUserId())){
                return sendResult200();
            }else{
                return sendResult(OrderConstans.RESPONSE_CODE_600,OrderConstans.RESPONSE_MESSAGE_600);
            }
        }else{
            return sendResult400();
        }
    }

    /**
     * 取消订单
     * @param orderId      订单id
     * @param cancel 取消订单原因
     * @return
     */
    @RequestMapping(value = "/cancelOrder")
    public BaseResult cancelOrder(String orderId,String cancel) {
        if(StringUtils.isNoneBlank(orderId,cancel)){
            if(iOrderGoodsService.cancelOrder(orderId,cancel,getUserId())){
                return sendResult200();
            }else{
                return sendResult(OrderConstans.RESPONSE_CODE_600,OrderConstans.RESPONSE_MESSAGE_600);
            }
        }else{
            return sendResult400();
        }
    }

}
